package web;

import com.alibaba.fastjson.JSON;
import pojo.Brand;
import service.brandService.brandService;
import service.brandServiceImpl.brandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectAllServlet")
public class selectAllServlet extends HttpServlet {
    private brandService brandservice =new brandServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Brand> brands = brandservice.selectAll();//调用service里的方法
        String jsonString = JSON.toJSONString(brands);//转化为json对象以便传输给axios作为响应数据
        resp.setContentType("text/json;charset=utf-8");//传输数据里有中文，防止乱码
//        resp.setHeader("Access- Control-Allow-Origin","*");//跨域问题暂时用不上
        resp.getWriter().write(jsonString);//作为响应数据传输给axios
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
